package com.ocelot.san.converter;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import com.ocelot.san.dto.Lawyer;
import com.ocelot.san.service.OperationService;

@ManagedBean 
@RequestScoped 
public class LawyerConverter implements Converter {

  	@ManagedProperty("#{operationService}")
    private OperationService operationService;
 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
        try {
        	Long id = Long.valueOf(value);
        	return operationService.findLawyerById(id);
        } catch (Exception e) { 
            throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to OperationType", value)), e); 
        } 
    }
    
 
    public String getAsString(FacesContext context, UIComponent component, Object value) {
    	if(value!=null) {
	    	if(value instanceof Lawyer) {
	    		Lawyer lawyer = (Lawyer) value;
	            return String.valueOf(lawyer.getId()); 
	    	}
    		return value.toString();
    	}
    	return null;
    }

	public OperationService getOperationService() {
		return operationService;
	}

	public void setOperationService(OperationService operationService) {
		this.operationService = operationService;
	}
 
}

 
 
